﻿@{
    ViewBag.Title = "供应商列表";
}

<div class="row">
    <div class="col-xs-12">
        <p>
            <div class="row">
                <div class="col-sm-6">
                    <div class="input-group-btn">
                        状态:
                        <select id="query-status">
                            <option value="1" selected="selected">正常</option>
                            <option value="2">已禁用</option>
                            <option value="3">已删除</option>
                        </select>
                        <a class="btn btn-sm btn-primary" href="javascript:void(0)" id="btnCreate">
                            <i class="icon-plus bigger-110"></i>创建
                        </a>
                        <button class="btn btn-sm btn-danger" id="btnDelete">
                            <i class="icon-remove bigger-110"></i>删除
                        </button>
                    </div>
                </div>
                <div class="col-sm-6 right">
                </div>
            </div>
        </p>
        <div class="table-responsive">
            <table id="businessTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" /></th>
                        <th>Code</th>
                        <th>名称</th>
                        <th>类型</th>
                        <th>金额</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                @*<button type="button" class="close"
                        data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>*@
                <h4 class="modal-title" id="myModalLabel">
                    创建优惠项目
                </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form" id="formNew">
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 编号 </label>
                        <div class="col-sm-9">
                            <input type="text" id="newCode" placeholder="输入编号..." class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 名称 </label>
                        <div class="col-sm-9">
                            <input type="text" id="newName" placeholder="输入名称..." class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1">类型</label>
                        <div class="col-sm-9">
                            <select id="newType">
                                <option value="1" selected="selected">付费项</option>
                                <option value="2">优惠项</option>
                            </select>
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 金额 </label>
                        <div class="col-sm-9">
                            <input type="text" id="newAmount" placeholder="输入金额..." class="col-xs-10 col-sm-8" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 描述 </label>
                        <div class="col-sm-9">
                            <textarea id="newDescription" placeholder="描述文字..." rows="3" class="col-xs-10 col-sm-8" style="resize:none"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button>
                <button type="button" class="btn btn-primary" id="btnSave">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
@section inlineScript
{
    <script type="text/javascript">
        var oTable = null;
        $(document).ready(function () {
            $('#com-search-input').bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    queryAdvertisementList();
                }
            });
            $('#com-search-icon').on('click', function () {
                queryAdvertisementList();
            });
            $('#query-status').on('change', function () {
                queryAdvertisementList();
            });
            $('#selectAll').on('change', function () {
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        $(element).prop('checked', false);
                    } else {
                        $(element).prop('checked', true);
                    }
                });
            });
            $('#btnDelete').on('click', function () {
                var idArray = [];
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        idArray.push($(element).attr('data-id'));
                    }
                });
                onDeleteMany(idArray);
            });
            $('#btnCreate').on('click', function () {
                $('#formNew')[0].reset();
                $('#myModal').modal({
                    keyboard: true
                })
            });
            $('#btnSave').on('click', function () {
                onSave();
            });

            queryAdvertisementList();
        });

        function queryAdvertisementList() {
            if (oTable) {
                oTable.fnClearTable();
            }
            oTable = $("#businessTable").dataTable({
                bRetrieve: true,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 20,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "@Url.Content("~/API/ASMS/Query/PayItemPagedList")",//这个是请求的地址
                "fnServerData": retrieveData, // 获取数据的处理函数
                "aoColumns": [
                    {
                        //"sTitle": "Name",
                        "sWidth": '40px',
                        mData: null,
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            if (full.status == 3) {
                                return "";
                            }
                            return '<input type="checkbox" name="selectItem" data-id="' + full.id + '"/>';
                        }
                    },
                    {
                        mData: "code",
                        bSortable: false
                    },
                    {
                        mData: "name",
                        bSortable: false
                    },
                    {
                        mData: "type",
                        bSortable: false,
                        mRender: function (data, type, full) {
                            if (data == 1) {
                                return "付费项";
                            } else if (data == 2) {
                                return "优惠项";
                            }
                            return 'unknown';
                        }
                    },
                    {
                        mData: "amount",
                        bSortable: false,
                        mRender: function (data, type, full) {
                            if (full.type == 1) {
                                return '<font style="color:red">' + data + '</font>';
                            } else if (full.type == 2) {
                                return '<font style="color:green">-' + data + '</font>';
                            }
                            return 'unknown';
                        }
                    },
                    {
                        bSortable: false,
                        mData: "status",
                        mRender: function (data, type, full) {
                            if (data == 1) {
                                return "正常";
                            } else if (data == 2) {
                                return "已禁用";
                            } else if (data == 3) {
                                return "已删除";
                            }
                            return 'unknown';
                        }
                    },
                    {
                        mData: "createdTime",
                        bSortable: false
                    },
                    {
                        //"sTitle": "Operation",
                        mData: null,
                        "sWidth": '200px',
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            if (full.status == 3) {
                                return "";
                            }
                            return '<a href="javascript:void(0)" onclick="onDelete(this);" data-id="' + full.id + '">删除</a>';
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        }

        // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
        function retrieveData(sSource, aoData, fnCallback) {
            var start = 0;
            var limit = 0;
            var propertyNames = new Array();
            var sortIndex = 0;
            var sortDirection;
            for (var index = 0; index < aoData.length; index++) {
                if (aoData[index].name == 'iDisplayStart') {
                    start = aoData[index].value;
                } else if (aoData[index].name == 'iDisplayLength') {
                    limit = aoData[index].value;
                } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                    propertyNames.push(aoData[index].value);
                } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                    sortIndex = aoData[index].value;
                } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                    sortDirection = aoData[index].value;
                }
            }
            $.ajax({
                url: sSource,//这个就是请求地址对应sAjaxSource
                data: {
                    "status": $('#query-status').val(),
                    'start': start,
                    'limit': limit
                },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        fnCallback({
                            sEcho: aoData[0].value,
                            iTotalRecords: result.data.total,
                            iTotalDisplayRecords: result.data.total,
                            aaData: result.data.items
                        });
                    }
                },
                error: function (msg) {
                }
            });
        }

        function onDelete(el) {
            var id = $(el).attr('data-id');  // save
            if (!id) {
                return;
            }
            if (window.confirm("是否要删除项目?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Delete/PayItem")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'id': id }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };
        function onDeleteMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要删除选中的项目?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Delete/PayItemArray")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        }

        function onSave() {
            var code = $('#newCode').val();
            var name = $('#newName').val();
            var type = $('#newType').val();
            var amount = $('#newAmount').val();
            var description = $('#newDescription').val();
            if (code && name && type && amount) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Create/PayItem")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'code': code, 'name': name, 'type': type, 'amount': amount, 'description': description }),
                    success: function (data) {
                        if (data.isSuccess) {
                            $('#myModal').modal('hide');
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        }
    </script>
}
